package com.caonima.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.caonima.factory.DaoFactory;
import com.caonima.util.WebFormatter;
import com.caonima.vo.News;

public class AddNews extends HttpServlet {

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String nid = request.getParameter("nid");
		String type = request.getParameter("type");
		String title = request.getParameter("title");
		String editor = request.getParameter("editor");
		String keyeditor = request.getParameter("keyeditor");
		String content = request.getParameter("content");
		String source = request.getParameter("source");
		News news = new News();
		
		news.setTitle(title);
		news.setEditor(editor);
		news.setKeyeditor(keyeditor);
		news.setContent(content);
		news.setTextContent(WebFormatter.html2text(content));
		news.setSource(source);
		news.setType(type);
		news.setActive(false);
		news.setPubdate(new Date());
		if("twjs".equals(type)) {
			int index = 0;
			String imgdir = null;
			if((index = content.indexOf("img ")) != -1) {
				
				imgdir = content.substring(index+9);
				if((index = imgdir.indexOf("ueditor/jsp/")) != -1) {
					imgdir = imgdir.substring(index);
					imgdir = imgdir.substring(0, imgdir.indexOf("\""));
					news.setImgdir(imgdir);
					news.setPicnews(true);
				}
			} else {
				news.setImgdir("");
				news.setPicnews(false);
			}
		}
		boolean flag = false;
		if(!(nid ==null || "".equals(nid))) {
			news.setNid(Integer.parseInt(nid));
			try {
				flag = DaoFactory.getINewsDaoInstance().doUpdate(news);
			} catch (Exception e) {
				e.printStackTrace();
			}
			if(flag == true) {
				request.getRequestDispatcher("./news?nid=" + nid).forward(request, response);
			}
		} else {
			try {
				flag = DaoFactory.getINewsDaoInstance().doCreate(news);
			} catch (Exception e) {
				e.printStackTrace();
			}
			if(flag == true) {
				request.getRequestDispatcher("./list?type=" + type).forward(request, response);
			}
		}
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doGet(request, response);
	}

}
